package com.xyx.www;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Test0731 {
	public static void main(String[] args) {
		//testEx14();
		//testEx15();
		try {
			testEx16();
		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("请输入数字");
			testEx16();
		}
	}

	/**
	 * 捕获异常
	 */
	public static void testEx14() {
		try {
			int appleNum = 0;
			int stuNum = 0;
			Scanner sc = new Scanner(System.in);
			System.out.println("输入有多少个苹果");
			appleNum = sc.nextInt();
			System.out.println("输入有多少个孩子");
			stuNum = sc.nextInt();
			System.out.println("每个孩子分得：" + appleNum / stuNum);
		} catch (NullPointerException e) {
			// TODO: handle exception
			System.out.println("发生异常,请输入数字");
			testEx14();
		}
		
	}
	/**
	 * 手动抛出异常
	 */
	public static void testEx15() {
		
		try {
			int appleNum = 0;
			int stuNum = 0;
			Scanner sc = new Scanner(System.in);
			System.out.println("输入有多少个苹果");
			appleNum = sc.nextInt();
			System.out.println("输入有多少个孩子");
			stuNum = sc.nextInt();
			System.out.println("每个孩子分得：" + appleNum / stuNum);
			//手动抛出一个异常
			throw new InputMismatchException("输入数字");
		} catch (InputMismatchException e) {
			// TODO: handle exception
			System.out.println("QQQQQQQQQQ");
		}
	}
	/**
	 * 声明异常
	 */
	public static void testEx16() throws NullPointerException {
		int appleNum = 0;
		int stuNum = 0;
		Scanner sc = new Scanner(System.in);
		System.out.println("输入有多少个苹果");
		appleNum = sc.nextInt();
		System.out.println("输入有多少个孩子");
		stuNum = sc.nextInt();
		System.out.println("每个孩子分得：" + appleNum / stuNum);
		throw new NullPointerException();
	}
}
